package top.lixb.libcommon.base;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/19.
 */

public class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragments      = new ArrayList<>();
    private final List<String>   mFragmentTitles = new ArrayList<>();
    private Fragment mCurFragment;

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);

    }

    public List<Fragment> getFragments() {
        return mFragments;
    }

    public Fragment getFragment(int pos) {
        return mFragments.get(pos);
    }


    public List<String> getFragmentTitles() {
        return mFragmentTitles;
    }

    public void removeAll() {
        mFragments.clear();
        mFragmentTitles.clear();
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitles.get(position);
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        if (object instanceof Fragment) {
            mCurFragment = (Fragment) object;
        }
        super.setPrimaryItem(container, position, object);
    }

    public Fragment getCurrentFragment() {
        return mCurFragment;
    }
}

